package main;


import base.BaseWindow;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginWindow extends BaseWindow {

    public static final String userName = "root";    //默认账号密码
    public static final String passwrod = "123456";

    private JLabel label1 = new JLabel("账号");
    private JLabel label2 = new JLabel("密码");
    private JTextField text1 = new JTextField();
    private JPasswordField text2 = new JPasswordField();
    private JButton button = new JButton("LoginWindow");

    @Override
    protected JFrame setRootView() {
        JFrame jFrame = new JFrame("成绩管理系统登陆");
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        jFrame.setBounds(((int) dimension.getWidth() - 200) / 2, ((int) dimension.getHeight() - 300) / 2, 275, 152);
        jFrame.setResizable(false);
        jFrame.setLayout(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jFrame;
    }

    @Override
    protected void initView(JFrame root) {
        label1.setBounds(10, 10, 100, 30);
        root.add(label1);

        label2.setBounds(10, 40, 100, 30);
        root.add(label2);

        text1.setBounds(50, 15, 180, 20);
        root.add(text1);

        text2.setBounds(50, 45, 180, 20);
        root.add(text2);

        button.setBounds(10, 75, 250, 40);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onLogin(root);
            }
        });
        root.add(button);
    }

    private void onLogin(JFrame root) {
        if (userName.equals(text1.getText()) && passwrod.equals(text2.getText())) {
            root.dispose();
            new HomeWindow().start();
        } else {
            JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
            text1.setText("");
            text2.setText("");
        }
    }

    @Override
    protected void loadData() {

    }
}
